using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Collections;

namespace RUPS
{
    public partial class VizualizareComponenteComandaForm : Form
    {
        public string idComanda;
        public string caleFisierCulori,
           caleFisierTipCanat,
           caleFisierPrivacyGlass;
        public string denCuloare, idCul;
        public static ArrayList
            tipCanat,
            tipSticla,
            tipPrivacyGlass, culori;
        public static string tipSticlaAles,
                        plasaInsecte,
                        tipPlasaInsecte,
            nrCamere,
            nrBucati,
            IDTipMaterial,
            lungime,
            latime,
            tipPGAles,
            tipCanatAles,
            idCompComAleasa;
                

        public VizualizareComponenteComandaForm()
        {
            InitializeComponent();
        }

        private void VizualizareComponenteComandaForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'comenziClientiMasivDBDataSet1.comenziClienti' table. You can move, or remove it, as needed.
            this.comenziClientiTableAdapter1.Fill(this.comenziClientiMasivDBDataSet1.comenziClienti);
            // TODO: This line of code loads data into the 'masivDBDataSet14.componenteComenziClienti' table. You can move, or remove it, as needed.
            this.componenteComenziClientiTableAdapter.Fill(this.masivDBDataSet14.componenteComenziClienti);
            // TODO: This line of code loads data into the 'comenziCclientiMasivDBDataSet.comenziClienti' table. You can move, or remove it, as needed.
            this.comenziClientiTableAdapter.Fill(this.comenziCclientiMasivDBDataSet.comenziClienti);
            comboBox1.DataSource = null;
            comboBox1.Items.Clear();
            SqlDataAdapter da = new SqlDataAdapter("select idComanda from comenziClienti", comenziClientiTableAdapter.Connection);
            DataSet ds = new DataSet();
            da.Fill(ds, "com");
            comboBox1.DataSource = ds.Tables["com"];
            comboBox1.DisplayMember = "idComanda";
            comboBox1.ValueMember = "idComanda";
            


            //foreach (DataRow r in ds.Tables["com"].Rows)
            //{
            //    this.comboBox1.Items.Add(r["idComanda"].ToString());
            //}
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            idComanda = this.comboBox1.SelectedIndex.ToString();
            SqlDataAdapter da = new SqlDataAdapter("select * from comenziClienti where idComanda=" + idComanda, comenziClientiTableAdapter.Connection);
            DataSet ds = new DataSet();
            da.Fill(ds, "comenziClienti");

            SqlDataAdapter daCl = new SqlDataAdapter("select * from clienti where id=" + ds.Tables[0].Rows[0]["idClient"].ToString(), comenziClientiTableAdapter.Connection);
            DataSet dsCl = new DataSet();
            daCl.Fill(dsCl, "clienti");
            textBox1.Text += "Nume si prenume client: " + dsCl.Tables["clienti"].Rows[0]["nume"].ToString() + " " +
                dsCl.Tables["clienti"].Rows[0]["prenume"].ToString() + "\r\n";
            textBox1.Text += "Data comanda: " + ds.Tables[0].Rows[0]["dataComanda"].ToString() + "\r\n";
            textBox1.Text += "Data livrare: " + ds.Tables[0].Rows[0]["dataLivrare"].ToString();

            ////selectare componente comanda din BD pt afisarea lor in gridul de componente
            this.dataGridView1.DataSource = null;
            this.dataGridView1.Rows.Clear();
            SqlDataAdapter daComp = new SqlDataAdapter("select * from componenteComenziClienti where idComanda=" + idComanda,
                                    this.componenteComenziClientiTableAdapter.Connection);
            DataSet dsComp = new DataSet();
            daComp.Fill(dsComp, "comp");

            ////golire grid cu componente comanda
            if (this.dataGridView1.Rows.Count != 0)
            {
                this.dataGridView1.DataSource = null;
                this.dataGridView1.Rows.Clear();
            }

            if (dsComp.Tables["comp"] != null)
            {
                int nrRand = 0;
                foreach (DataRow r in dsComp.Tables["comp"].Rows)
                {
                    this.dataGridView1.Rows.Add();
                    dataGridView1.Rows[nrRand].Cells[0].Value = r["idComponentaComanda"].ToString();
                    dataGridView1.Rows[nrRand].Cells[1].Value = r["idTipMaterial"].ToString();
                    dataGridView1.Rows[nrRand].Cells[2].Value = r["lungime"].ToString();
                    dataGridView1.Rows[nrRand].Cells[3].Value = r["latime"].ToString();

                    //detectarea culorii materialului
                    SqlDataAdapter daTipMat = new SqlDataAdapter("select idCuloare from tipMaterial where id=" + r["idTipMaterial"].ToString(), comenziClientiTableAdapter.Connection);
                    DataSet dsTipMat = new DataSet();
                    daTipMat.Fill(dsTipMat, "tipMat");
                    if (dsTipMat.Tables["tipMat"] != null)
                    {
                        idCul = dsTipMat.Tables["tipMat"].Rows[0]["IDculoare"].ToString();

                        //caut denumirea culorii
                        SqlDataAdapter daCul = new SqlDataAdapter("select numeCuloare from culori where id=" + idCul, comenziClientiTableAdapter.Connection);
                        daCul.Fill(dsTipMat, "denCul");
                        if (dsTipMat.Tables["denCul"] != null)//exista culoarea
                        {
                            denCuloare = dsTipMat.Tables["denCul"].Rows[0]["numeCuloare"].ToString();
                        }
                    }
                    string pathFileCuloare = caleFisierCulori + "v" + idCul + ".jpg";

                    //selectarea culorii pp construirea unei imagini bitmap
                    Image bitmap = new Bitmap(pathFileCuloare);
                    dataGridView1.Rows[nrRand].Cells[4].Value = new Bitmap(bitmap);
                    dataGridView1.Rows[nrRand].Cells[5].Value = r["tipCanat"].ToString();
                    dataGridView1.Rows[nrRand].Cells[6].Value = r["tipPravacyGlass"].ToString();
                    if (r["tipPravacyGlass"].ToString() != "")
                    {
                        caleFisierPrivacyGlass = AppDomain.CurrentDomain.BaseDirectory + "privacy glass\\";
                        string pathFilePrivacyGlass = caleFisierPrivacyGlass;
                        pathFilePrivacyGlass += r["tipPravacyGlass"].ToString();
                        pathFilePrivacyGlass += ".bmp";
                        bitmap = new Bitmap(pathFilePrivacyGlass);
                        dataGridView1.Rows[nrRand].Cells[7].Value = new Bitmap(bitmap);
                    }
                    else
                    {
                        string pathFilePrivacyGlass = caleFisierPrivacyGlass + "nimic.bmp";
                        bitmap = new Bitmap(pathFilePrivacyGlass);
                        dataGridView1.Rows[nrRand].Cells[7].Value = new Bitmap(bitmap);
                    }
                    dataGridView1.Rows[nrRand].Cells[8].Value = r["plasaInsecte"].ToString();
                    dataGridView1.Rows[nrRand].Cells[9].Value = r["tipPlasaInsecte"].ToString();
                    dataGridView1.Rows[nrRand].Cells[10].Value = r["tipSticla"].ToString();
                    dataGridView1.Rows[nrRand].Cells[11].Value = r["nrCamere"].ToString();
                    dataGridView1.Rows[nrRand].Cells[12].Value = r["nrBucati"].ToString();
                    dataGridView1.Rows[nrRand].Cells[13].Value = r["IDComanda"].ToString();
                    nrRand += 1;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            //this.textBox1.Text = "";
            //this.textBox1.Text = "";
            //if(comboBox1.SelectedItem!=null)
            //if (this.comboBox1.SelectedItem.ToString() != "")
            //{
            //    idComanda = this.comboBox1.SelectedText.ToString();

            //    SqlDataAdapter da = new SqlDataAdapter("select * from comenziCLienti where idComanda=" + idComanda.ToString(),
            //        comenziClientiTableAdapter1.Connection);
            //    DataSet ds = new DataSet();
            //    da.Fill(ds, "com");

            //    SqlDataAdapter daCl = new SqlDataAdapter("select * from clienti where id=" + ds.Tables[0].Rows[0]["idClient"].ToString(), comenziClientiTableAdapter.Connection);
            //    DataSet dsCl = new DataSet();
            //    daCl.Fill(dsCl, "clienti");
            //    textBox1.Text += "Nume si prenume client: " + dsCl.Tables["clienti"].Rows[0]["nume"].ToString() + " " +
            //        dsCl.Tables["clienti"].Rows[0]["prenume"].ToString() + "\r\n";
            //    textBox1.Text += "Data comanda: " + ds.Tables[0].Rows[0]["dataComanda"].ToString() + "\r\n";
            //    textBox1.Text += "Data livrare: " + ds.Tables[0].Rows[0]["dataLivrare"].ToString();

            //    ////selectare componente comanda din BD pt afisarea lor in gridul de componente
            //    this.dataGridView1.DataSource = null;
            //    this.dataGridView1.Rows.Clear();
            //    SqlDataAdapter daComp = new SqlDataAdapter("select * from componenteComenziClienti where idComanda=" + idComanda,
            //                            this.comenziClientiTableAdapter.Connection);
            //    DataSet dsComp = new DataSet();
            //    daComp.Fill(dsComp, "comp");

            //    ////golire grid cu componente comanda
            //    if (this.dataGridView1.Rows.Count != 0)
            //    {
            //        this.dataGridView1.DataSource = null;
            //        this.dataGridView1.Rows.Clear();
            //    }

            //    if (dsComp.Tables["comp"] != null)
            //    {
            //        int nrRand = 0;
            //        foreach (DataRow r in dsComp.Tables["comp"].Rows)
            //        {
            //            this.dataGridView1.Rows.Add();
            //            dataGridView1.Rows[nrRand].Cells[0].Value = r["idComponentaComanda"].ToString();
            //            dataGridView1.Rows[nrRand].Cells[1].Value = r["idTipMaterial"].ToString();
            //            dataGridView1.Rows[nrRand].Cells[2].Value = r["lungime"].ToString();
            //            dataGridView1.Rows[nrRand].Cells[3].Value = r["latime"].ToString();

            //            //detectarea culorii materialului
            //            SqlDataAdapter daTipMat = new SqlDataAdapter("select idCuloare from tipMaterial where id=" + r["idTipMaterial"].ToString(), comenziClientiTableAdapter.Connection);
            //            DataSet dsTipMat = new DataSet();
            //            daTipMat.Fill(dsTipMat, "tipMat");
            //            if (dsTipMat.Tables["tipMat"] != null)
            //            {
            //                idCul = dsTipMat.Tables["tipMat"].Rows[0]["IDculoare"].ToString();

            //                //caut denumirea culorii
            //                SqlDataAdapter daCul = new SqlDataAdapter("select numeCuloare from culori where id=" + idCul, comenziClientiTableAdapter.Connection);
            //                daCul.Fill(dsTipMat, "denCul");
            //                if (dsTipMat.Tables["denCul"] != null)//exista culoarea
            //                {
            //                    denCuloare = dsTipMat.Tables["denCul"].Rows[0]["numeCuloare"].ToString();
            //                }
            //            }
            //            string pathFileCuloare = caleFisierCulori + "v" + idCul + ".jpg";

            //            //selectarea culorii pp construirea unei imagini bitmap
            //            Image bitmap = new Bitmap(pathFileCuloare);
            //            dataGridView1.Rows[nrRand].Cells[4].Value = new Bitmap(bitmap);
            //            dataGridView1.Rows[nrRand].Cells[5].Value = r["tipCanat"].ToString();
            //            dataGridView1.Rows[nrRand].Cells[6].Value = r["tipPravacyGlass"].ToString();
            //            if (r["tipPravacyGlass"].ToString() != "")
            //            {
            //                caleFisierPrivacyGlass = AppDomain.CurrentDomain.BaseDirectory + "privacy glass\\";
            //                string pathFilePrivacyGlass = caleFisierPrivacyGlass;
            //                pathFilePrivacyGlass += r["tipPravacyGlass"].ToString();
            //                pathFilePrivacyGlass += ".bmp";
            //                bitmap = new Bitmap(pathFilePrivacyGlass);
            //                dataGridView1.Rows[nrRand].Cells[7].Value = new Bitmap(bitmap);
            //            }
            //            else
            //            {
            //                string pathFilePrivacyGlass = caleFisierPrivacyGlass + "nimic.bmp";
            //                bitmap = new Bitmap(pathFilePrivacyGlass);
            //                dataGridView1.Rows[nrRand].Cells[7].Value = new Bitmap(bitmap);
            //            }
            //            dataGridView1.Rows[nrRand].Cells[8].Value = r["plasaInsecte"].ToString();
            //            dataGridView1.Rows[nrRand].Cells[9].Value = r["tipPlasaInsecte"].ToString();
            //            dataGridView1.Rows[nrRand].Cells[10].Value = r["tipSticla"].ToString();
            //            dataGridView1.Rows[nrRand].Cells[11].Value = r["nrCamere"].ToString();
            //            dataGridView1.Rows[nrRand].Cells[12].Value = r["nrBucati"].ToString();
            //            dataGridView1.Rows[nrRand].Cells[13].Value = r["IDComanda"].ToString();
            //            nrRand += 1;
            //        }
            //    }
            //}
            
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            
            this.textBox1.Text = "";
            if (comboBox1.SelectedItem != null)
                if (this.comboBox1.SelectedItem.ToString() != ""
                    && this.comboBox1.SelectedText.ToString()!="")
                {
                    idComanda = this.comboBox1.SelectedText.ToString();

                    SqlDataAdapter da = new SqlDataAdapter("select * from comenziCLienti where idComanda=" + idComanda.ToString(),
                        comenziClientiTableAdapter1.Connection);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "com");

                    SqlDataAdapter daCl = new SqlDataAdapter("select * from clienti where id=" + ds.Tables[0].Rows[0]["idClient"].ToString(), comenziClientiTableAdapter.Connection);
                    DataSet dsCl = new DataSet();
                    daCl.Fill(dsCl, "clienti");
                    textBox1.Text += "Nume si prenume client: " + dsCl.Tables["clienti"].Rows[0]["nume"].ToString() + " " +
                        dsCl.Tables["clienti"].Rows[0]["prenume"].ToString() + "\r\n";
                    textBox1.Text += "Data comanda: " + ds.Tables[0].Rows[0]["dataComanda"].ToString() + "\r\n";
                    textBox1.Text += "Data livrare: " + ds.Tables[0].Rows[0]["dataLivrare"].ToString();

                    ////selectare componente comanda din BD pt afisarea lor in gridul de componente
                    this.dataGridView1.DataSource = null;
                    this.dataGridView1.Rows.Clear();
                    SqlDataAdapter daComp = new SqlDataAdapter("select * from componenteComenziClienti where idComanda=" + idComanda,
                                            this.comenziClientiTableAdapter.Connection);
                    DataSet dsComp = new DataSet();
                    daComp.Fill(dsComp, "comp");

                    ////golire grid cu componente comanda
                    if (this.dataGridView1.Rows.Count != 0)
                    {
                        this.dataGridView1.DataSource = null;
                        this.dataGridView1.Rows.Clear();
                    }

                    if (dsComp.Tables["comp"] != null)
                    {
                        int nrRand = 0;
                        foreach (DataRow r in dsComp.Tables["comp"].Rows)
                        {
                            this.dataGridView1.Rows.Add();
                            dataGridView1.Rows[nrRand].Cells[0].Value = r["idComponentaComanda"].ToString();
                            dataGridView1.Rows[nrRand].Cells[1].Value = r["idTipMaterial"].ToString();
                            dataGridView1.Rows[nrRand].Cells[2].Value = r["lungime"].ToString();
                            dataGridView1.Rows[nrRand].Cells[3].Value = r["latime"].ToString();

                            //detectarea culorii materialului
                            SqlDataAdapter daTipMat = new SqlDataAdapter("select idCuloare from tipMaterial where id=" + r["idTipMaterial"].ToString(), comenziClientiTableAdapter.Connection);
                            DataSet dsTipMat = new DataSet();
                            daTipMat.Fill(dsTipMat, "tipMat");
                            if (dsTipMat.Tables["tipMat"] != null)
                            {
                                idCul = dsTipMat.Tables["tipMat"].Rows[0]["IDculoare"].ToString();

                                //caut denumirea culorii
                                SqlDataAdapter daCul = new SqlDataAdapter("select numeCuloare from culori where id=" + idCul, comenziClientiTableAdapter.Connection);
                                daCul.Fill(dsTipMat, "denCul");
                                if (dsTipMat.Tables["denCul"] != null)//exista culoarea
                                {
                                    denCuloare = dsTipMat.Tables["denCul"].Rows[0]["numeCuloare"].ToString();
                                }
                            }
                            string pathFileCuloare = caleFisierCulori + "v" + idCul + ".jpg";

                            //selectarea culorii pp construirea unei imagini bitmap
                            Image bitmap = new Bitmap(pathFileCuloare);
                            dataGridView1.Rows[nrRand].Cells[4].Value = new Bitmap(bitmap);
                            dataGridView1.Rows[nrRand].Cells[5].Value = r["tipCanat"].ToString();
                            dataGridView1.Rows[nrRand].Cells[6].Value = r["tipPravacyGlass"].ToString();
                            if (r["tipPravacyGlass"].ToString() != "")
                            {
                                caleFisierPrivacyGlass = AppDomain.CurrentDomain.BaseDirectory + "privacy glass\\";
                                string pathFilePrivacyGlass = caleFisierPrivacyGlass;
                                pathFilePrivacyGlass += r["tipPravacyGlass"].ToString();
                                pathFilePrivacyGlass += ".bmp";
                                bitmap = new Bitmap(pathFilePrivacyGlass);
                                dataGridView1.Rows[nrRand].Cells[7].Value = new Bitmap(bitmap);
                            }
                            else
                            {
                                string pathFilePrivacyGlass = caleFisierPrivacyGlass + "nimic.bmp";
                                bitmap = new Bitmap(pathFilePrivacyGlass);
                                dataGridView1.Rows[nrRand].Cells[7].Value = new Bitmap(bitmap);
                            }
                            dataGridView1.Rows[nrRand].Cells[8].Value = r["plasaInsecte"].ToString();
                            dataGridView1.Rows[nrRand].Cells[9].Value = r["tipPlasaInsecte"].ToString();
                            dataGridView1.Rows[nrRand].Cells[10].Value = r["tipSticla"].ToString();
                            dataGridView1.Rows[nrRand].Cells[11].Value = r["nrCamere"].ToString();
                            dataGridView1.Rows[nrRand].Cells[12].Value = r["nrBucati"].ToString();
                            dataGridView1.Rows[nrRand].Cells[13].Value = r["IDComanda"].ToString();
                            nrRand += 1;
                        }
                    }
                }
        }
    }
}